/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.standardValues.StandardParams;
import systole.domain.clinicalInformation.standardValues.StandardValue;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.StandardValuesBroker;

/**
 *
 * @author jmj
 */
public class StandardValuesBrokerDB extends BrokerDB implements StandardValuesBroker {

    /**
     * 
     */
    public StandardValuesBrokerDB() {
        super();
    }

    public List<StandardValue> getAllStandardValues() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all standard values");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(StandardValue.class).addOrder(Order.asc("param"));
            @SuppressWarnings("unchecked")
            List<StandardValue> standardValues = criteria.list();
            this.logger.logDebug("getting standard values successfully");
            return standardValues;
        } catch (HibernateException e) {
            this.logger.logError("error on get all standard values, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los valores por defecto", e.fillInStackTrace());
        }
    }

    public StandardValue getStandardValueByParamType(StandardParams paramType) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting standard value by paramType, " + paramType);
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(StandardValue.class).add(Restrictions.eq("param", paramType)).setMaxResults(1);
            StandardValue standardValue = (StandardValue) criteria.uniqueResult();
            this.logger.logDebug("getting standard value successfully");
            return standardValue;
        } catch (HibernateException e) {
            this.logger.logError("error on get standard value by paramType, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los valores por defecto", e.fillInStackTrace());
        }
    }

    public void initSatandardValues() throws ExceptionDAO {

        for (StandardParams p : StandardParams.values()) {
            StandardValue standardValue = new StandardValue();
            standardValue.setParam(p);
            standardValue.setNormal(p.getNormal());
            standardValue.setMedium(p.getMedium());
            standardValue.setHigh(p.getHigh());
            this.insert(standardValue);
        }
    }

    public void insert(StandardValue standardValue) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving standard values, " + standardValue);
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(standardValue);
            this.logger.logDebug("save standard values successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save standard value, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public void update(StandardValue standardValue) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating standard values, " + standardValue);
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(standardValue);
            this.logger.logDebug("update standard values successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update standard value, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }
}
